<?php
/**
 *  申请支付
 */
    class ApplyPay extends APIBase {
		
		public $tag = "ApplyPay";
// 		public $isLogin = false;
		
		public function before() {
			$this->initMysql();
			return true;
		}
		
		//生成随机24位订单号
		protected function createOrderId ($length = 24) {
			$seed=md5(microtime());
			$pwd='';
			for($i=0;$i<$length;$i++){
				$pwd.=$seed{mt_rand(0,31)};
			}
			$hash=md5($pwd);
			return substr($hash,0,$length);
		}
		
		public function logic() {
			if (!isset($this->param['channel'])) {
				$this->returnError(310, "channel must be needed");
			}
			
			if (!isset($this->param['ua'])) {
				$this->returnError(310, "ua must be needed");
			}
			
			
			if (!isset($this->param['imsi'])) {
				$this->returnError(310, "imsi must be needed");
			}
			
			if (!isset($this->param['imei'])) {
				$this->returnError(310, "imei must be needed");
			}
			
			if (!isset($this->param['version'])) {
				$this->returnError(310, "param must be needed");
			}
			
			if (!isset($this->param['cid'])) {
				$this->returnError(310, "cid must be needed");
			}
			
			if (!isset($this->param['goodsid'])) {
				$this->returnError(310, "goodsid must be needed");
			}
			
			if (!isset($this->param['price'])) {
				$this->returnError(310, "price must be needed");
			}
			
			if (!isset($this->param['pay_type'])) {
				$this->returnError(310, "pay_type must be needed");
			}
			
			if (!isset($this->param['user'])) {
				$this->returnError(310, "user must be needed");
			}
				
			if (!isset($this->param['ipaddress'])) {
				$this->returnError(310, "ipaddress must be needed");
			}
				
			if (!isset($this->param['mac'])) {
				$this->returnError(310, "mac must be needed");
			}

            $where['id'] = (int)$this->param['goodsid'];
            $good = $this->mysql->select('goods','*',$where);

            if(!$good[0])
                $this->returnError(310, "goodsid is not exists");

// 			file_put_contents('a.txt',$this->uid);
			$payinfo = $this->param;
			$payinfo['uid'] = $this->uid;
// 			file_put_contents('price.txt',$this->param['price']);
			$payinfo['price'] = (int)$good[0]['rmb'];
			$payinfo['pay_type'] = $this->param['pay_type'];
			$payinfo['goodsid'] = (int)$payinfo['goodsid'];
			$payinfo['create_time'] = time();
			$payinfo['type'] = 0;
			$payinfo['is_send'] = 0;
			$payinfo['orderid'] = $this->createOrderId();
			$sign  = $this->mysql->insert('applypay', $payinfo);
			if ($sign){
				$data = array();
				$data['orderid'] = $payinfo['orderid'];
				
				$notyurl = "";
				switch ($this->param['pay_type'])
				{
					//爱贝短代支付
					case 10:
						$notyurl= Config::$ab_pay_confing['notify_url'];
                        $signstr=Config::$ab_pay_confing["app_id"]
                            ."&"
                            .$this->param['goodsid']
                            ."&".$good[0]['name']
                            ."&"
                            .($payinfo['price']*100)
                            ."&1&"
                            .$payinfo['orderid']
                            ."&"
                            .$notyurl
                            ."&"
                            .Config::$ab_pay_confing["app_key"];

                       // echo $signstr."\n";
                        $data["sign"]=md5($signstr);
                        //echo  $data["sign"];
                        $data["app_id"]=Config::$ab_pay_confing["app_id"];
						break;
					default:
						break;
				}
				
				$data['notify_url'] = $notyurl;
				
				$this->returnData($data);
			}else{
				$this->returnError(301,'create order failed!');
			}
		}
		
		public function after() {
			$this->deinitMysql();
		}



         /*
        sign=MD5(app_id&goods_id&goods_name&unit_price&quantity&user_order_id&notify_ur
        l&app_key)*/
    }
?>
